<template>
  <view class="product-waterfall">
    <view class="waterfall-wrapper">
      <view class="waterfall-column">
        <view v-for="(item, index) in leftProducts" :key="index" class="product-item">
          <image :src="item.logo" mode="widthFix" @load="onImageLoad"></image>
          <view class="product-info">
            <text class="product-name">{{item.name}}</text>
            <view class="product-price">
              <text class="price">¥{{item.price}}</text>
              <text class="sold">已售{{item.sold}}件</text>
            </view>
          </view>
        </view>
      </view>
      <view class="waterfall-column">
        <view v-for="(item, index) in rightProducts" :key="index" class="product-item">
          <image :src="item.logo" mode="widthFix" @load="onImageLoad"></image>
          <view class="product-info">
            <text class="product-name">{{item.name}}</text>
            <view class="product-price">
              <text class="price">¥{{item.price}}</text>
              <text class="sold">已售{{item.sold}}件</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {post} from "../../../common/api";

const leftProducts = ref([])
const rightProducts = ref([])

// 模拟商品数据

// 图片加载完成后重新计算高度
const onImageLoad = () => {
  // 可以在这里添加高度计算逻辑
}

onMounted(async () => {

  let res = await post("/product/list", {size: 21});
  if (res.code!==200){
    return;
  }
  let mockProducts=res.data;
  // 将商品数据分配到左右两列
  mockProducts.forEach((item, index) => {
    if (index % 2 === 0) {
      leftProducts.value.push(item)
    } else {
      rightProducts.value.push(item)
    }
  })
})
</script>

<style>
.product-waterfall {
  padding: 20rpx;
}

.waterfall-wrapper {
  display: flex;
  justify-content: space-between;
}

.waterfall-column {
  width: 48%;
}

.product-item {
  background: #fff;
  border-radius: 12rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
}

.product-item image {
  width: 100%;
}

.product-info {
  padding: 16rpx;
}

.product-name {
  font-size: 28rpx;
  color: #333;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.product-price {
  margin-top: 12rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  color: #ff5500;
  font-size: 32rpx;
  font-weight: bold;
}

.sold {
  color: #999;
  font-size: 24rpx;
}
</style>
